import pygame
from pygame.locals import *
try:
    import android
except ImportError:
    android = None
if android:
    import android.mixer
    
from constants import *

class Sound:
    def __init__(self):
        self.sounds = {} # S_ code to sound

    def initialize(self):
        if android:
            android.mixer.init()
        else:
            pygame.mixer.init()            
        self.add(S_HIT,'files/hit.wav') # Hit a monster in melee
        self.add(S_MISS,'files/miss.wav') # Miss a monster in melee or missile
        self.add(S_ZAP,'files/zap.wav') # Hit a monster with a wand, or a monster zaps you
        self.add(S_SHOOT,'files/shoot.wav') # Hit a monster with a bow-type weapon, or a monster shoots you
        self.add(S_MONHIT,'files/monhit.wav') # Monster hits you in melee
        self.add(S_MONMISS,'files/monmiss.wav') # Monster misses you in melee or missile
        self.add(S_CANCEL,'files/cancel.wav') # UI
        self.add(S_KEY,'files/key.wav') # UI
        self.add(S_OUCH,'files/character_killed.wav') # Used when a character incapacitated
        # self.add(S_MONSTER_OUCH,?) # Used when a monster killed. At present a kind of skreeling sound
        self.add(S_GOOD_SPELL,'files/good_spell.wav') # Many character abilities
        self.add(S_GOLD,'files/gold.wav') # Treasure
        self.add(S_BAD_SPELL,'files/bad_spell.wav') # Many monster abilities
        self.add(S_BREATH,'files/breath.wav') # Fire breath, soft enrage, explode
        self.add(S_SPIT,'files/spit.wav') # Poison spit
        self.add(S_POTION,'files/potion.wav') # Drink a potion - Dark dwarf also
        self.add(S_DECONSTRUCT,'files/deconstruct.wav')
        self.add(S_EXTINGUISH,'files/extinguish.wav')
        self.add(S_FANFARE,'files/fanfare.wav')
        self.add(S_FIRE_BOLT,'files/fire_bolt.wav')
        self.add(S_GAS,'files/gas.wav')
        self.add(S_HEALING,'files/healing.wav')
        self.add(S_ICE_BOLT,'files/ice_bolt.wav')
        self.add(S_LIGHTNING_BOLT,'files/lightning_bolt.wav')
        self.add(S_SMITE,'files/smite.wav')
        self.add(S_UNLOCKING,'files/unlocking.wav')
        self.add(S_ITEM,'files/item.wav')
        self.add(S_WIND,'files/wind.wav')
        self.add(S_DOOR,'files/door.wav')
        self.add(S_STAIRS,'files/footsteps.wav')           

    def add(self,s_code,filename):
        if android:
            self.sounds[s_code] = android.mixer.Sound(filename)
        else:
            self.sounds[s_code] = pygame.mixer.Sound(filename)

    def play(self,s_code):
        if s_code in self.sounds.keys():
            if self.sounds[s_code] is not None:
                self.sounds[s_code].play(0)

s = Sound()
